#include "mainwindow.h"
#include "picshow.h"
#include "protree.h"
#include "protreewidget.h"
#include "ui_mainwindow.h"
#include <QMenu>
#include <QAction>
#include <QKeySequence>
#include <wizard.h>
#include <QTreeWidget>
#include <QFileDialog>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QMenu * menu_file = new QMenu(tr("文件(&F)"));
    ui->menubar->addMenu(menu_file);

    QAction * create_pro = new QAction(tr("新建项目"));
    create_pro->setIcon(QIcon(":/icon/createpro.png"));
    create_pro->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_N));
    menu_file->addAction(create_pro);

    QAction * open_pro = new QAction(tr("打开项目"));
    open_pro->setIcon(QIcon(":/icon/openpro.png"));
    menu_file->addAction(open_pro);

    QMenu * menu_setting = new QMenu(tr("设置(&S)"));
    ui->menubar->addMenu(menu_setting);



    connect(create_pro,&QAction::triggered,this,&MainWindow::create_pro_slot);

    _protree = new ProTree(this);
    ui->prolayout->addWidget(_protree);

    _picshow = new PicShow();
    ui->piclayout->addWidget(_picshow);

    //选择的图片显示在右边。
    QTreeWidget* tree_widget = dynamic_cast <ProTree*>(_protree)->GetTreeWidget();
    auto * pro_tree_widget = dynamic_cast<ProTreeWidget*>(tree_widget);
    auto * pro_pic_show = dynamic_cast<PicShow*>(_picshow);
    //ProTreeWidget双击图片就会发送SigUpdateSelected
    connect(pro_tree_widget, &ProTreeWidget::SigUpdateSelected,pro_pic_show,
            &PicShow::SlotSelectItem);
    connect(pro_pic_show, &PicShow::SigPreClicked,pro_tree_widget,&ProTreeWidget::SlotPreShow);
    connect(pro_pic_show, &PicShow::SigNextClicked,pro_tree_widget,&ProTreeWidget::SlotNextShow);
    connect(pro_tree_widget,&ProTreeWidget::SigUpdatePic,pro_pic_show,&PicShow::SlotUpdatePic);

    //连接打开项目的槽函数
    connect(open_pro, &QAction::triggered, this, &MainWindow::SlotOpenPro);
    connect(this, &MainWindow::SigOpenPro, pro_tree_widget, &ProTreeWidget::SlotOpenPro);

    connect(pro_tree_widget,&ProTreeWidget::SigClearSelected,pro_pic_show,
            &PicShow::SlotClearSelected);

    QAction * act_music = new QAction(QIcon(":/icon/music.png"), tr("背景音乐"),this);
    act_music->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_M));
    menu_setting->addAction(act_music);
    //设置音乐
    connect(act_music, &QAction::triggered, pro_tree_widget, &ProTreeWidget::SlotSetMusic);
}

//mainwindow的大小改变图像也应该改变。
void MainWindow::resizeEvent(QResizeEvent *event)
{

    auto * pro_pic_show = dynamic_cast<PicShow*>(_picshow);
    pro_pic_show->ReloadPic();
    QMainWindow::resizeEvent(event);
}


MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::create_pro_slot(bool)
{
    qDebug() << "创建项目";
    Wizard wizard;
    auto * page0 = wizard.page(0);
    auto * page1 = wizard.page(1);
    wizard.setWindowTitle(tr("新建项目"));
    //这里基类指针确实指向派生类dynamic_cast是安全的。
    connect(&wizard,&Wizard::ProSetSignal,dynamic_cast<ProTree*>(_protree),&ProTree::AddToTree);
    page0->setTitle(tr("项目配置"));
    page1->setTitle(tr("完成"));
    wizard.show();
    wizard.exec();
    //断开MainWindow的所有信号,有什么具体作用吗？只断开mainwindow作为发送者的连接
    disconnect();
}

void MainWindow::SlotOpenPro(bool)
{
    QFileDialog file_dialog;
    file_dialog.setFileMode(QFileDialog::Directory);
    file_dialog.setWindowTitle("选择导入的文件夹");

    file_dialog.setDirectory(QDir::currentPath());
    file_dialog.setViewMode(QFileDialog::Detail);

    QStringList fileNames;
    if (file_dialog.exec()) {
        fileNames = file_dialog.selectedFiles();
    }

    if (fileNames.length() <= 0) {
        return;
    }

    QString import_path = fileNames.at(0);
    qDebug() << "import_path is " << import_path;
    emit SigOpenPro(import_path);
}
